package cn.sciento.transfer.api.controller.v1;

import cn.sciento.core.domain.Page;
import cn.sciento.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import cn.sciento.boot.platform.lov.annotation.ProcessLovValue;
import cn.sciento.core.base.BaseController;
import cn.sciento.core.util.Results;
import cn.sciento.mybatis.helper.SecurityTokenHelper;
import cn.sciento.starter.keyencrypt.core.Encrypt;
import cn.sciento.transfer.app.service.ConsTenantConfigService;
import cn.sciento.transfer.domain.entity.ConsTenantConfig;
import cn.sciento.transfer.domain.repository.ConsTenantConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = {"Consumer Tenant Config (Site Level)"})
@RestController("consTenantConfigSiteController.v1")
@RequestMapping({"/v1/tenant-cons"})
public class ConsTenantConfigSiteController extends BaseController {
  @Autowired
  private ConsTenantConfigRepository consTenantConfigRepository;
  
  @Autowired
  private ConsTenantConfigService consTenantConfigService;
  
  @ApiOperation("删除数据消息消费租户配置")
  @Permission(level = ResourceLevel.SITE)
  @DeleteMapping
  public ResponseEntity removeConsTenantConfig(@Encrypt @RequestBody List<ConsTenantConfig> consTenantConfigs) {
    SecurityTokenHelper.validToken(consTenantConfigs);
    consTenantConfigs.forEach(item -> this.consTenantConfigRepository.deleteByPrimaryKey(item.getConsTenantConfigId()));
    return Results.success();
  }
  
  @ApiOperation("分页数据消息消费租户配置")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping({"/{consDbConfigId}"})
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<Page<ConsTenantConfig>> listConsTenantConfig(@Encrypt ConsTenantConfig consTenantConfig, @ApiIgnore @SortDefault(value = {"consTenantConfigId"}, direction = Sort.Direction.DESC) PageRequest pageRequest, @Encrypt @PathVariable("consDbConfigId") Long consDbConfigId) {
    consTenantConfig.setConsDbConfigId(consDbConfigId);
    Page<ConsTenantConfig> list = this.consTenantConfigService.pageAndSortConsTenantConfig(pageRequest, consTenantConfig);
    return Results.success(list);
  }
}
